﻿using com.guojin.core.io.message;
using System.Collections;
using System.Collections.Generic;
using com.guojin.mj.net.message.game;
using UnityEngine;
using DG.Tweening;

public class ChapterEndController : MonoBehaviour
{
    private static ChapterEndController instance;

    public static ChapterEndController GetInstance()
    {
        return instance;
    }
    void Awake()
    {
        instance = this;
    }

    public void OnReceviceMsg129(string msg, Message data)
    {
        ChapterInfoRet cir = (ChapterInfoRet)data;

        int i = 0;
        GameRoomModel.GetInstance().isfinishGame = false;

        foreach (var item in cir.userChapterInfo)//为是不是完成最终游戏的游戏状态赋值
        {
            Debug.Log(item.BetState);
            if (item.BetState == 0)
            {
                i++;
                if (i >= 2)
                {
                    GameRoomModel.GetInstance().isfinishGame = true;
                }
            }
        }



        ChapterEndSetChapterRoomInfo(cir);

        ChapterEndSetJvGuoAni(cir.isJvGuo);

        ChapterEndSetChapterUserInfo(cir);

        if (!cir.isJvGuo || GameRoomModel.GetInstance().CurrentChapter == GameRoomModel.GetInstance().MaxChapter)
        {
            GetChipAni(cir.winIndex);
        }

        ShowChapterPage(cir);
    }

    public void ShowChapterPage(ChapterInfoRet cir)
    {
        StartCoroutine(ShowChapterPageLater(cir));
    }

    IEnumerator ShowChapterPageLater(ChapterInfoRet cir)
    {
        yield return new WaitForSeconds(2f);

        ChapterEndController.GetInstance().ShowChapterEndPage(cir);

        ChapterEndController.GetInstance().ChapterEndCheckReadyBtn();
    }

    private void ChapterEndSetChapterRoomInfo(ChapterInfoRet cir)
    {
        if (GameRoomModel.GetInstance().RoomState != 2)
        {
            GameRoomModel.GetInstance().RoomState = 2;
        }

        GameRoomModel.GetInstance().IsJvGuo = cir.isJvGuo;

        GameRoomModel.GetInstance().MaxLimitScore = cir.MaxLimitScore;

        GameRoomModel.GetInstance().CurrentSingleBet = cir.currentSingleBet;

        GameRoomModel.GetInstance().CurrentTotalBet = cir.currentTotalBet;
    }

    private void ChapterEndSetJvGuoAni(bool isJvGuo)
    {
        if (isJvGuo)
        {
            GameRoomPage.GetInstance().SetAudAndPlay(4);

            GameRoomPage.GetInstance().SetJvGuoAni(true);
        }
    }

    public void ChapterEndSetChapterUserInfo(ChapterInfoRet cir)
    {
        for (int i = 0; i < cir.userChapterInfo.Count; i++)
        {
            Player player = GameRoomModel.GetInstance().playerDic[cir.userChapterInfo[i].LocationIndex];
            player.UserTotalScore = cir.userChapterInfo[i].UserTotalScore;
            player.BetState = cir.userChapterInfo[i].BetState;
            player.winCounts = cir.userChapterInfo[i].WinCounts;
            player.defeatCounts = cir.userChapterInfo[i].DefeatCounts;
            if (cir.isJvGuo)
            {
                if (((IList)cir.winIndex).Contains(player.locationIndex))
                {
                    player.PlayerState = 3;
                }
                else if (player.BetState == 5)
                {
                    player.isOut = true;
                    player.PlayerState = 4;
                }
                else if (!((IList)cir.winIndex).Contains(player.locationIndex))
                {
                    player.PlayerState = 2;
                }
            }
            else
            {
                if (((IList)cir.winIndex).Contains(player.locationIndex))
                {
                    player.PlayerState = 1;
                }
                else if (player.BetState == 5)
                {
                    player.isOut = true;
                    player.PlayerState = 4;
                }
                else if (!((IList)cir.winIndex).Contains(player.locationIndex))
                {
                    player.PlayerState = 2;
                }
            }

          
                if (!player.isOut)//显示手牌
                {
                    Debug.Log("玩家不是弃牌状态" + player.locationIndex);
                    int localState = GameRoomModel.GetInstance().playerDic[GameData.GetInstance().locationIndex].BetState;//当前玩家的状态

                    if (localState != 5)
                    {
                        player.ShowCard();
                        player.ShowAllCardType();
                    }
                    if (GameRoomModel.GetInstance().isfinishGame) //完成游戏所有人的牌和分都显示出来
                    {
                    player.ShowCard();
                    player.ShowAllCardType();
                }
                }
        }
    }

    private void GetChipAni(int[] winArr)
    {
        GameObject obj = GameObject.Find("DeskBetPoint").gameObject;
        if (obj)
        {
            GameRoomPage.GetInstance().SetAudAndPlay(0);
            while (obj.transform.childCount > 0)
            {
                int temp = winArr[Random.Range(0, winArr.Length)];
                var tempObj = obj.transform.GetChild(0);
                tempObj.SetParent(GameRoomModel.GetInstance().playerDic[temp].playerHeadObj.transform);
                var tweener = tempObj.DOMove(GameRoomModel.GetInstance().playerDic[temp].playerHeadObj.transform.position, 0.5f);
                tweener.OnComplete(() => Destroy(tempObj.gameObject));
            }
        }
    }

    public void ShowChapterEndPage(ChapterInfoRet cir)
    {
        UIManager.ChangeUI(UIManager.PageState.ChapterEndPage, (GameObject obj) =>
        {
            obj.GetComponent<ChapterEndPage>().InformationSetting();
        });

        SetRoomModelJvGuo(cir.isJvGuo);

        SetChapterPageBg(cir.isJvGuo, cir.winIndex);

        LoadChapterEndItem(cir.userChapterInfo);
    }

    public void ChapterEndCheckReadyBtn()
    {
        //显示准备按钮
        if (GameRoomModel.GetInstance().CurrentChapter < GameRoomModel.GetInstance().MaxChapter)
        {
            GameRoomPage.GetInstance().SetReadyBtn(1);
        }
    }

    private void SetRoomModelJvGuo(bool isJvGuo)
    {
        if (isJvGuo)
        {
            GameRoomModel.GetInstance().IsJvGuo = true;
        }
        else
        {
            GameRoomModel.GetInstance().IsJvGuo = false;
        }
    }

    private void SetChapterPageBg(bool isJvGuo, int[] winArr)
    {
        if (isJvGuo)
        {
            ChapterEndPage.GetInstance().SetBg(2);
        }
        else
        {
            Debug.Log(GameData.GetInstance().locationIndex);
            Debug.Log(winArr);
            var isWinner = ((IList)winArr).Contains(GameData.GetInstance().locationIndex);

            if (isWinner)
            {
                ChapterEndPage.GetInstance().SetBg(1);
            }
            else
            {
                ChapterEndPage.GetInstance().SetBg(0);
            }
        }
    }

    private void LoadChapterEndItem(List<UserChapterInfoRet> ucir)
    {
        for (int i = 0; i < ucir.Count; i++)
        {
            var temp = TooL.LoadPrefab(ChapterEndPage.GetInstance().group, "chapterEndItem");
            temp.GetComponent<ChapterEndItemPage>().InitChapterEndItem(ucir[i]);
        }
    }
}
